use actix_files::NamedFile;
use actix_web::{App, HttpResponse, HttpServer, Responder, Result, get, mime, post, web};
use backend::{get_config};
use log::info;
use log4rs;
use std::path::Path;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    log4rs::init_file("log4rs.yaml", Default::default())
        .expect("无法加载日志配置文件，请确认.config目录下存在log4rs.yaml");
    let config = get_config();
    info!("EasyDrop 后端服务已启动 🏗️...");
    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(echo)
            .service(web::scope("/app")
                .route("/hello", web::get().to(manual_hello))
            )   
    })
    .bind(("0.0.0.0", config.server.port))?
    .workers(6) // 设置工作线程数为6
    .run()
    .await
}
#[get("/")]
async fn hello() -> Result<NamedFile> {
    let path = Path::new("public/index.html");
    Ok(NamedFile::open(path)?
        .set_content_type(mime::TEXT_HTML_UTF_8)
        .use_last_modified(true)
        .use_etag(true)
        .disable_content_disposition())
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}
